package com.lyc.interceptor;

import com.lyc.pojo.Users;
import com.lyc.tool.SYS;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created by luoxinmin on 2020/9/15.
 */
public class AdminLoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
            HttpSession session = httpServletRequest.getSession();
            Object obj = session.getAttribute(SYS.SYS_USER);//User对象
            if (obj != null && obj instanceof Users) {
                //放行
            return true;
        }
        //跳转到登陆
        httpServletResponse.sendRedirect("/admin/admin/adminLogin");//去后台登录界面
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
        System.out.println(o + "\t" + modelAndView.getModel() + "\t" + modelAndView.getView());
//        modelAndView.addObject("",)
//        modelAndView.setViewName("test");
    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
        System.out.println("afterCompletion");
    }
}
